package com.ruoyi.central.controller;

import java.util.List;

import com.ruoyi.common.validation.group.InsertGroup;
import com.ruoyi.common.validation.group.UpdateGroup;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.utils.bean.SelectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.central.domain.TopCentralControllerDeviceType;
import com.ruoyi.central.service.ITopCentralControllerDeviceTypeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.validation.group.UpdateBatchGroup;
import com.ruoyi.common.core.domain.StateVo;
import java.util.Collection;
/**
 * 设备类型Controller
 * 
 * @author ruoyi
 * @date 2022-02-25
 */
@Controller
@RequestMapping("/central/centralControllerDeviceType")
public class TopCentralControllerDeviceTypeController extends BaseController
{
    private String prefix = "central/centralControllerDeviceType";

    @Autowired
    private ITopCentralControllerDeviceTypeService topCentralControllerDeviceTypeService;

    @RequiresPermissions("central:centralControllerDeviceType:view")
    @GetMapping()
    public String centralControllerDeviceType()
    {
        return prefix + "/centralControllerDeviceType";
    }

    /**
     * 查询设备类型列表
     */
    @RequiresPermissions("central:centralControllerDeviceType:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(TopCentralControllerDeviceType topCentralControllerDeviceType)
    {
        startPage();
        List<TopCentralControllerDeviceType> list = topCentralControllerDeviceTypeService.selectList(topCentralControllerDeviceType);
        return getDataTable(list);
    }

    /**
     * 查询简单的设备类型列表 用于select选择器
     */
    @GetMapping("/simpleList")
    @ResponseBody
    public AjaxResult simpleList(TopCentralControllerDeviceType topCentralControllerDeviceType){

        Collection<TopCentralControllerDeviceType> topCentralControllerDeviceTypeList = topCentralControllerDeviceTypeService.selectAllMap().values();

        return AjaxResult.success(SelectUtils.buildSelectList(topCentralControllerDeviceTypeList,TopCentralControllerDeviceType::getId,TopCentralControllerDeviceType::getDeviceTypeTitle));
    }

    /**
     * 获取设备类型详细信息
     */
    @RequiresPermissions("central:centralControllerDeviceType:query")
    @GetMapping(value = "/{id}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(topCentralControllerDeviceTypeService.selectById(id));
    }

    /**
     * 获取自增排序值
     * @param topCentralControllerDeviceType 过滤条件
     * @return
     */
    @GetMapping("/getIncrementWeight")
    @ResponseBody
    public AjaxResult getIncrementWeight(TopCentralControllerDeviceType topCentralControllerDeviceType){
        int weigth = topCentralControllerDeviceTypeService.getMaxOfWeight(topCentralControllerDeviceType);
        return AjaxResult.success(++weigth);
    }

    /**
     * 下载设备类型导入模板
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<TopCentralControllerDeviceType> util = new ExcelUtil<>(TopCentralControllerDeviceType.class);
        util.importTemplateExcel(response, "设备类型数据");
    }

    /**
     * 导入设备类型数据
     * @param file 设备类型数据文件
     * @param updateSupport 是否为更新
     */
    @Log(title = "设备类型", businessType = BusinessType.IMPORT)
    @RequiresPermissions("central:centralControllerDeviceType:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TopCentralControllerDeviceType> util = new ExcelUtil<>(TopCentralControllerDeviceType.class);
        List<TopCentralControllerDeviceType> topCentralControllerDeviceTypeList = util.importExcel(file.getInputStream());

        if(updateSupport){
            if(topCentralControllerDeviceTypeService.updateBatchById(topCentralControllerDeviceTypeList)){
                return AjaxResult.success("导入设备类型数据覆盖成功！");
            }
            return AjaxResult.error("导入设备类型数据覆盖失败！");
        }
        if(topCentralControllerDeviceTypeService.insertBatch(topCentralControllerDeviceTypeList)){
            return AjaxResult.success("导入设备类型数据新增成功！");
        }
        return AjaxResult.error("导入设备类型数据新增失败！");
    }

    /**
     * 导出设备类型列表
     */
    @RequiresPermissions("central:centralControllerDeviceType:export")
    @Log(title = "设备类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TopCentralControllerDeviceType topCentralControllerDeviceType)
    {
        List<TopCentralControllerDeviceType> list = topCentralControllerDeviceTypeService.selectList(topCentralControllerDeviceType);
        ExcelUtil<TopCentralControllerDeviceType> util = new ExcelUtil<TopCentralControllerDeviceType>(TopCentralControllerDeviceType.class);
        return util.exportExcel(list, "设备类型数据");
    }

    /**
     * 新增设备类型
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存设备类型
     */
    @RequiresPermissions("central:centralControllerDeviceType:add")
    @Log(title = "设备类型", businessType = BusinessType.INSERT)
    @PostMapping
    @ResponseBody
    public AjaxResult addSave(@Validated(InsertGroup.class) @RequestBody TopCentralControllerDeviceType topCentralControllerDeviceType)
    {
        return toAjax(topCentralControllerDeviceTypeService.insert(topCentralControllerDeviceType));
    }

    /**
     * 修改设备类型
     */
    @RequiresPermissions("central:centralControllerDeviceType:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        TopCentralControllerDeviceType topCentralControllerDeviceType = topCentralControllerDeviceTypeService.selectById(id);
        mmap.put("topCentralControllerDeviceType", topCentralControllerDeviceType);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备类型
     */
    @RequiresPermissions("central:centralControllerDeviceType:edit")
    @Log(title = "设备类型", businessType = BusinessType.UPDATE)
    @PutMapping
    @ResponseBody
    public AjaxResult editSave(@Validated(UpdateGroup.class) @RequestBody TopCentralControllerDeviceType topCentralControllerDeviceType)
    {
        return toAjax(topCentralControllerDeviceTypeService.updateById(topCentralControllerDeviceType));
    }

    /**
     * 修改设备类型 状态 根据 ids 主键
     */
    @RequiresPermissions("central:centralControllerDeviceType:edit")
    @Log(title = "设备类型", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStateByIds")
    @ResponseBody
    public AjaxResult updateStateByIds(@Validated(value= UpdateBatchGroup.class)@RequestBody StateVo<Integer> stateVo)
    {
        return toAjax(topCentralControllerDeviceTypeService.updateStateByIds(stateVo.getIds(),stateVo.getState()));
    }

    /**
     * 恢复设备类型 为未删除 根据 ids 主键
     */
    @RequiresPermissions("central:centralControllerDeviceType:restore")
    @Log(title = "设备类型", businessType = BusinessType.UPDATE)
    @PutMapping("/restoreData/{ids}")
    @ResponseBody
    public AjaxResult recoverByIds(@PathVariable("ids") List<Integer> ids){
        return toAjax(topCentralControllerDeviceTypeService.recoverBatchByIds(ids));
    }

    /**
     * 删除设备类型
     */
    @RequiresPermissions("central:centralControllerDeviceType:remove")
    @Log(title = "设备类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ResponseBody
    public AjaxResult deleteByIds(@PathVariable("ids") List<Integer> ids)
    {
            return toAjax(topCentralControllerDeviceTypeService.logicDeleteBatchByIds(ids));
    }
}
